<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Album Model
 * @description : Database functions related to albums, album, photos
 * 
 */
class Album_model extends CI_Model {

  function __construct() {
    parent::__construct();
  }

  // Create new album
  function set_album($album_name = NULL, $album_user_id = NULL, $album_status = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($album_name) && !empty($album_user_id) && is_numeric($album_user_id)) {
      $query_insr = array(
          'album_name' => ucwords($album_name),
          'album_user_id' => $album_user_id,
          'album_date' => date("Y-m-d H:i:s"),
          'album_status' => $album_status
      );
      $query_exec = $this->db->insert('albums', $query_insr);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result']['album_id'] = $this->db->insert_id();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Get all albums or albums of a user
  function get_albums($user_id = NULL, $limit = 12, $offset = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    if (!empty($user_id) && is_numeric($user_id)) {
      $this->db->where("album_user_id = '$user_id' AND (album_status = '1' OR album_status = '2')");
    } else {
      $this->db->where("album_status = '1'");
    }
    $this->db->select('albums.*, users.*', FALSE);
    $this->db->select('(SELECT photo_file FROM albums_photos WHERE photo_album_id = albums.album_id ORDER BY RAND() LIMIT 1) AS album_cover_photo', FALSE);
    $this->db->join('users', 'users.user_id = albums.album_user_id');
    $this->db->order_by('album_id', 'desc');
    $query_exec = $this->db->get_where('albums', $query_cond, $limit, $offset);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->result_array();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  // Get albums count
  function get_albums_count($user_id = NULL, $limit = 8) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    $this->db->select('count(*) AS total_count', FALSE);
    if (!empty($user_id) && is_numeric($user_id)) {
      $this->db->where("album_user_id = '$user_id' AND (album_status = '1' OR album_status = '2')");
    } else {
      $this->db->where("album_status = '1'");
    }
    $this->db->order_by('album_id', 'desc');
    $query_exec = $this->db->get_where('albums', $query_cond, $limit);
    if ($query_exec == TRUE) {
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->row()->total_count;
      $result_data['count'] = $query_exec->row()->total_count;
    }
    return $result_data;
  }

  // Add photo to a album
  function add_photo($album_id = NULL, $image_file = NULL, $photo_video_type = 0) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($image_file) && !empty($album_id) && is_numeric($album_id)) {
      $query_insr = array(
          'photo_album_id' => $album_id,
          'photo_video_type' => $photo_video_type,
          'photo_file' => $image_file,
      );
      $query_exec = $this->db->insert('albums_photos', $query_insr);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result']['album_id'] = $this->db->insert_id();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Get album by id
  function get_album($album_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($album_id) && is_numeric($album_id)) {
      $query_cond = array();
      $query_cond['album_id'] = $album_id;
      // $query_cond['album_status'] = 1;
      $this->db->join('users', 'user_id = album_user_id');
      $query_exec = $this->db->get_where('albums', $query_cond);
      if ($query_exec == TRUE) {
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get all album items count
  function get_album_items_count($album_id = NULL, $user_id = NULL, $photo_video_type = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    $query_cond = array();
    if (!empty($album_id) && is_numeric($album_id)) {
      $query_cond['album_id'] = $album_id;
    }
    $query_cond['photo_video_type'] = (int) $photo_video_type;
    if (!empty($user_id) && is_numeric($user_id)) {
      $this->db->where("album_user_id = '$user_id' AND (album_status = '1' OR album_status = '2')");
    } else {
      $this->db->where("album_status = '1'");
    }
    $this->db->join('albums', 'album_id = photo_album_id');
    $query_exec = $this->db->get_where('albums_photos', $query_cond);
    if ($query_exec == TRUE) {
      $result_data['status'] = 1;
      $result_data['result'] = $query_exec->num_rows();
      $result_data['count'] = $query_exec->num_rows();
    }
    return $result_data;
  }

  // Get album photos by album id
  function get_photos($album_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($album_id) && is_numeric($album_id)) {
      $query_cond = array();
      $query_cond['photo_album_id'] = $album_id;
      $this->db->select('albums_photos.*', FALSE);
      $this->db->select('(SELECT COUNT(like_id) FROM likes WHERE like_type_id = albums_photos.photo_id AND like_type = "photo") AS photo_likes_count', FALSE);
      $this->db->select('(SELECT COUNT(comment_id) FROM comments WHERE comment_type_id = albums_photos.photo_id AND comment_type = "photo" AND comment_status = 1) AS photo_comments_count', FALSE);
      $this->db->order_by('photo_id', 'desc');
      $query_exec = $this->db->get_where('albums_photos', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Delete an album
  function delete_album($album_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($album_id) && is_numeric($album_id)) {
      $query_cond = array();
      $query_cond['photo_album_id'] = $album_id;
      $this->db->delete('albums_photos', $query_cond);
      $query_cond = array();
      $query_cond['album_id'] = $album_id;
      $this->db->delete('albums', $query_cond);
      $result_data = array();
      $result_data['status'] = 1;
      $result_data['result'] = 1;
      $result_data['count'] = 1;
    }
    return $result_data;
  }

  // Update album details
  function update_album($album_id = NULL, $album_name = NULL, $album_status = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($album_name) && !empty($album_id) && is_numeric($album_id)) {
      $query_upda = array('album_name' => ucwords($album_name), 'album_status' => $album_status);
      $query_cond = array('album_id' => $album_id);
      $query_exec = $this->db->update('albums', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Get photo by photo id and album id
  function get_photo($photo_id = NULL, $album_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($photo_id) && is_numeric($photo_id) && !empty($album_id) && is_numeric($album_id)) {
      $query_cond = array();
      $query_cond['photo_id'] = $photo_id;
      $query_cond['photo_album_id'] = $album_id;
      $query_exec = $this->db->get_where('albums_photos', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get album by id
  function get_album_by_id($album_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($album_id) && is_numeric($album_id)) {
      $query_cond = array();
      $query_cond['album_id'] = $album_id;
      $query_cond['album_status'] = 1;
      $this->db->join('users', 'user_id = album_user_id');
      $query_exec = $this->db->get_where('albums', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get photo by id
  function get_photo_by_id($photo_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($photo_id) && is_numeric($photo_id)) {
      $query_cond = array();
      $query_cond['photo_id'] = $photo_id;
      $this->db->join('albums', 'album_id = photo_album_id');
      $this->db->join('users', 'user_id = album_user_id');
      $query_exec = $this->db->get_where('albums_photos', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get photo details including album details by photo id
  function get_photo_details($photo_id = NULL, $photo_video_type = 0) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($photo_id) && is_numeric($photo_id)) {
      $query_cond = array();
      $query_cond['photo_id'] = $photo_id;
      if (!empty($photo_video_type)) {
        $query_cond['photo_video_type'] = $photo_video_type;
      }
      $this->db->join('albums', 'album_id = photo_album_id');
      $this->db->join('users', 'user_id = album_user_id');
      $query_exec = $this->db->get_where('albums_photos', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get all photos of a user
  function get_my_photos($user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id)) {
      $query_cond = array();
      $query_cond['album_user_id'] = $user_id;
      $this->db->join('albums', 'album_id = photo_album_id');
      $this->db->join('users', 'user_id = album_user_id');
      $query_exec = $this->db->get_where('albums_photos', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get previous photo in album by photo and album id
  function get_prev_photo_id($photo_id = NULL, $album_id = NULL, $photo_video_type = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($photo_id) && is_numeric($photo_id) && !empty($album_id) && is_numeric($album_id)) {
      $query_cond = array();
      $query_cond['photo_id < '] = $photo_id;
      //$query_cond['photo_video_type'] = $photo_video_type;
      $query_cond['photo_album_id'] = $album_id;
      $this->db->order_by('photo_id', 'desc');
      $query_exec = $this->db->get_where('albums_photos', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get next photo in album by photo and album id
  function get_next_photo_id($photo_id = NULL, $album_id = NULL, $photo_video_type = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($photo_id) && is_numeric($photo_id) && !empty($album_id) && is_numeric($album_id)) {
      $query_cond = array();
      $query_cond['photo_id > '] = $photo_id;
      //$query_cond['photo_video_type'] = $photo_video_type;
      $query_cond['photo_album_id'] = $album_id;
      $this->db->order_by('photo_id', 'asc');
      $query_exec = $this->db->get_where('albums_photos', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Delete photo using photo & album id
  function delete_photo($photo_id = NULL, $album_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($photo_id) && is_numeric($photo_id) && !empty($album_id) && is_numeric($album_id)) {
      $query_cond = array();
      $query_cond['photo_id'] = $photo_id;
      $query_cond['photo_album_id'] = $album_id;
      $query_exec = $this->db->delete('albums_photos', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Update album notificaion status 
  function upd_album_notify_status($album_id = NULL, $notify_status = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($album_id) && is_numeric($album_id)) {
      $query_cond = array();
      $query_cond['album_id'] = $album_id;
      $query_upda = array();
      $query_upda['album_notification'] = $notify_status;
      $query_exec = $this->db->update('albums', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Update album details
  function update_photo_desc($photo_id = NULL, $photo_desc = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($photo_desc) && !empty($photo_id) && is_numeric($photo_id)) {
      $query_upda = array('photo_desc' => $photo_desc);
      $query_cond = array('photo_id' => $photo_id);
      $query_exec = $this->db->update('albums_photos', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = 1;
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

}

?>
